package com.dabai.uitools.utils;

import android.app.Activity;
import android.view.View;
import android.view.animation.AlphaAnimation;

/**
 * Description : Other
 *
 * @author BAI
 */
public class AnimationUtils {



    /**
     * 视图淡入
     */
    public static void viewFadeIn(View view, int duration) {

        AlphaAnimation mShowAnimation;

        if (null == view || duration < 0) {
            return;
        }

        if (view.getVisibility() == View.VISIBLE) {
            return;
        }

        mShowAnimation = new AlphaAnimation(0.0f, 1.0f);
        mShowAnimation.setDuration(duration);
        mShowAnimation.setFillAfter(true);
        view.startAnimation(mShowAnimation);

        view.setVisibility(View.VISIBLE);
    }

    /**
     * 视图淡出
     */
    public static void viewFadeOut(Activity activity, View view, int duration, boolean gone) {

        if (null == view || duration < 0 || activity == null) {
            return;
        }

        if (view.getVisibility() != View.VISIBLE) {
            return;
        }

        new Thread(() -> {

            AlphaAnimation mHideAnimation;

            mHideAnimation = new AlphaAnimation(1.0f, 0.0f);
            mHideAnimation.setDuration(duration);
            mHideAnimation.setFillAfter(true);

            AlphaAnimation finalMHideAnimation = mHideAnimation;
            activity.runOnUiThread(() -> view.startAnimation(finalMHideAnimation));

            try {
                Thread.sleep(duration - 300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            activity.runOnUiThread(() -> {

                if (gone) {
                    view.setVisibility(View.GONE);
                } else {
                    view.setVisibility(View.INVISIBLE);
                }
            });

        }).start();


    }

}
